package com.m.sms.config;

import com.aliyun.dysmsapi20170525.Client;
import com.m.sms.properties.SmsProperties;
import com.m.sms.service.ISmsService;
import com.m.sms.service.impl.AliSmsServiceImpl;
import com.m.sms.service.impl.TencentSmsServiceImpl;
import com.m.sms.service.impl.YunpianSmsServiceImpl;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description
 *
 * @Auther [li.wang@jsxfedu.com]
 * @Date 2025/3/20 13:00
 * @Version 1.0
 * @Copyright (c) 2022 京师讯飞
 */
@Slf4j
@Configuration
public class SmsConfig {

    @Autowired
    private SmsProperties smsProperties;

    /**
     * 短信服务
     * @return
     */
    @Bean
    public ISmsService smsService(){
        if(smsProperties.getType().equals("ali")){
            return new AliSmsServiceImpl(aliyunClient());
        }else if(smsProperties.getType().equals("tencent")){
            return new TencentSmsServiceImpl(tencentClient());
        }
        return new YunpianSmsServiceImpl(smsProperties.getYunpian());
    }

    /**
     * 阿里云client配置
     * @return
     */
    private com.aliyun.dysmsapi20170525.Client aliyunClient() {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(smsProperties.getAli().getAccessKeyId())
                .setAccessKeySecret(smsProperties.getAli().getAccessKeySecret());
        config.endpoint = smsProperties.getAli().getEndpoint();
        try {
            return new Client(config);
        } catch (Exception e) {
            log.error("阿里云短信初始化失败", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 腾讯云client配置
     * @return
     */
    private SmsClient tencentClient() {
        com.tencentcloudapi.common.Credential credential = new com.tencentcloudapi.common.Credential(smsProperties.getTencent().getSecretId(),
                smsProperties.getTencent().getSecretKey());
        com.tencentcloudapi.common.profile.ClientProfile profile = new com.tencentcloudapi.common.profile.ClientProfile();

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("GET"); // get请求(默认为post请求)
        httpProfile.setConnTimeout(smsProperties.getTencent().getConnectTimeout()); // 请求连接超时时间，单位为秒(默认60秒)
        httpProfile.setWriteTimeout(smsProperties.getTencent().getWriteTimeout());  // 设置写入超时时间，单位为秒(默认0秒)
        httpProfile.setReadTimeout(smsProperties.getTencent().getReadTimeout());  // 设置读取超时时间，单位为秒(默认0秒)
        httpProfile.setEndpoint(smsProperties.getTencent().getEndpoint());
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod("HmacSHA256"); // 指定签名算法(默认为HmacSHA256)
        // 自3.1.80版本开始，SDK 支持打印日志。
        clientProfile.setHttpProfile(httpProfile);
        return new SmsClient(credential, smsProperties.getTencent().getRegion(), clientProfile);
    }
}
